package com.itheima.app.manager;


import cn.hutool.core.collection.CollUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.DataTotalService;
import com.itheima.service.mongo.FindUser;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class DataManager {

    @DubboReference
    DataTotalService dataTotalService;
    @DubboReference
    FindUser findUser;
    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    FriendService friendService;

    @Autowired
    HuanXinTemplate huanXinTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public ResponseEntity DataTotal() {
        //获取当前登陆者id
        Long userId = UserHolder.get().getId();
        Map<String, Integer> map = dataTotalService.DataTotal(userId);

        return ResponseEntity.ok(map);
    }

    //互相喜欢 喜欢 粉丝列表
    public ResponseEntity FindUserByPage(Integer pageNum, Integer pageSize, Integer type) {
        Long userId = UserHolder.get().getId();
        switch (type) {
            case 1: {
                PageBeanVo pageBeanVo = findUser.findEachLoveUserByPage(userId, pageNum, pageSize);
                List<Long> items = (List<Long>) pageBeanVo.getItems();

                List<UserLikeVo> voList = new ArrayList<>();

                if (CollUtil.isNotEmpty(items)) {

                    for (Long Id : items) {
                        UserInfo userInfo = userInfoService.findByUserId(Id);

                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(userInfo.getId());
                        vo.setAvatar(userInfo.getAvatar());
                        vo.setNickname(userInfo.getNickname());
                        vo.setGender(userInfo.getGender());
                        vo.setAge(userInfo.getAge());
                        vo.setCity(userInfo.getCity());
                        vo.setEducation(userInfo.getEducation());
                        vo.setMarriage(userInfo.getMarriage());

                        voList.add(vo);
                    }
                }
                pageBeanVo.setItems(voList);
                return ResponseEntity.ok(pageBeanVo);

            }
            case 2: {
                PageBeanVo pageBeanVo = findUser.findLoveUserByPage(userId, pageNum, pageSize);
                List<Long> items = (List<Long>) pageBeanVo.getItems();

                List<UserLikeVo> voList = new ArrayList<>();

                if (CollUtil.isNotEmpty(items)) {

                    for (Long Id : items) {
                        UserInfo userInfo = userInfoService.findByUserId(Id);

                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(userInfo.getId());
                        vo.setAvatar(userInfo.getAvatar());
                        vo.setNickname(userInfo.getNickname());
                        vo.setGender(userInfo.getGender());
                        vo.setAge(userInfo.getAge());
                        vo.setCity(userInfo.getCity());
                        vo.setEducation(userInfo.getEducation());
                        vo.setMarriage(userInfo.getMarriage());

                        voList.add(vo);
                    }
                }
                pageBeanVo.setItems(voList);
                return ResponseEntity.ok(pageBeanVo);
            }
            case 3: {
                PageBeanVo pageBeanVo = findUser.findFanUserByPage(userId, pageNum, pageSize);
                List<Long> items = (List<Long>) pageBeanVo.getItems();

                List<UserLikeVo> voList = new ArrayList<>();

                if (CollUtil.isNotEmpty(items)) {

                    for (Long Id : items) {
                        UserInfo userInfo = userInfoService.findByUserId(Id);

                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(userInfo.getId());
                        vo.setAvatar(userInfo.getAvatar());
                        vo.setNickname(userInfo.getNickname());
                        vo.setGender(userInfo.getGender());
                        vo.setAge(userInfo.getAge());
                        vo.setCity(userInfo.getCity());
                        vo.setEducation(userInfo.getEducation());
                        vo.setMarriage(userInfo.getMarriage());

                        voList.add(vo);
                    }
                }
                pageBeanVo.setItems(voList);
                return ResponseEntity.ok(pageBeanVo);
            }
            case 4:{
                PageBeanVo pageBeanVo = findUser.findVisitorByPage(userId, pageNum, pageSize);

                List<Visitor> items = (List<Visitor>) pageBeanVo.getItems();

                List<UserLikeVo> voList = new ArrayList<>();
                if (CollUtil.isNotEmpty(items)){
                    for (Visitor visitor : items) {
                        Long id = visitor.getVisitorUserId();
                        UserInfo userInfo = userInfoService.findByUserId(id);

                        UserLikeVo vo = new UserLikeVo();
                        vo.setId(userInfo.getId());
                        vo.setAvatar(userInfo.getAvatar());
                        vo.setNickname(userInfo.getNickname());
                        vo.setGender(userInfo.getGender());
                        vo.setAge(userInfo.getAge());
                        vo.setCity(userInfo.getCity());
                        vo.setEducation(userInfo.getEducation());
                        vo.setMarriage(userInfo.getMarriage());
                        vo.setMatchRate(visitor.getScore().intValue());

                        voList.add(vo);
                    }
                }
                pageBeanVo.setItems(voList);
                return ResponseEntity.ok(pageBeanVo);
            }
            default:
                return null;
        }
    }

    //粉丝-喜欢
    public ResponseEntity fansLove(Long fansId) {
        //获取登录者id
        Long userId = UserHolder.get().getId();
        //先把喜欢关系添加到userlike表中
        findUser.fansLove(userId,fansId);
        //添加数据库好友关系(动态表、好友表)
        friendService.saveContact(userId,fansId);

        //添加环信好友
        huanXinTemplate.addContacts("HX"+userId,"HX"+fansId);


        return ResponseEntity.ok(null);
    }

    //喜欢-取消
    public ResponseEntity cancleLove(Long friendId) {
        //获取当前登录者id
        Long userId = UserHolder.get().getId();
        //删除userlike表中的数据
        findUser.cancleLove(userId,friendId);
        //删除好友表数据(好友动态表 好友表)
        friendService.cancleFriend(userId,friendId);
        //删除环信好友关系
        huanXinTemplate.deleteContacts("HX"+userId,"HX"+friendId);



        return ResponseEntity.ok(null);
    }
}
